<template>
	<div v-show="rs" class="register display-none login-action">
		<div class="close clearfix">
			<a href="javascript:void(0)" @click=closeregister>×</a>
		</div>
		<h3>用户注册</h3>
		<form action="">
			<div class="com-list">
				<span>用户名：</span>
				<input type="text" placeholder="请输入用户名" v-model="rusername" data-index=1>
				<i>6-14位数字、字母组合</i>
			</div>
			<div class="wy-error uerror" data-index=1>{{ rusername_hint }}</div>
			<div class="com-list">
				<span>密码：</span>
				<input type="password" placeholder="请输入密码" v-model="rpassword" data-index=2>
				<i>6-14位数字、字母组合</i>
			</div>
			<div class="wy-error perror" data-index=2>{{ rpassword_hint }}</div>
			<div class="com-list">
				<span>手机号：</span>
				<input type="text" placeholder="请输入手机号" v-model="reginum" data-index=3>
				<i>11位手机号</i>
			</div>
			<div class="wy-error phonerror" data-index=3>{{ reginum_hint }}</div>
			<div class="com-list">
				<span>验证码：</span>
				<input type="text" placeholder="请输入验证码" style="width:175px;" v-model="identifying" data-index=4>
				<button @click="coding" type="button" :fgetc='fgetc' id="get-code">获取验证码</button>
				<i>获取验证码</i>
			</div>
			<div class="wy-error yeorror" data-index=4>{{ identifying_hint }}</div>
			<div class="com-list">
				<span>邀请码：</span>
				<input type="text" placeholder="请输入邀请人（选填）" v-model="inviter">
				<i>6位数字</i>
			</div>
			<div class="wy-error codeerror"></div>
			<div class="agree">
				<input type="checkbox">我已阅读并接受
				<a style="cursor:pointer;" @click="toAgree">《第一志愿用户协议》</a>
			</div>
			<button type="button" @click=checkver($event)>注册</button>
			<div class="has-account">
				已有账号
				<a href="javascript:void(0)" class="rightLogin" @click=rightLogin>立即登陆</a>
			</div>
		</form>
	</div>
</template>
<script>
	"use strict";

	module['base-register'] = {
		template: template,
		data: function data() {
			return {
				fgetc: true,
				rusername: '',
				rusername_hint: '',
				rpassword: '',
				rpassword_hint: '',
				reginum: '',
				reginum_hint:'',
				identifying: '',
				identifying_hint: '',
				inviter: '',
            	inviter_hint: '',
            	rs:1
			};
		},
		methods: {
			closeregister: function closeregister() {
				$(".register").hide();
				$(".cover-box").hide();
			},
			//发动验证码
			coding: function coding() {
				var _this = this;
				if(checkver_test.call(this)) {
					var phone = _this.reginum;
					if(phone == "") {
						_this.reginum_hint = "手机号不能为空";
						return;
					}
					base.request(base.api(38), {
						'phone': phone
					}, function(data) {
						if(_this.fgetc == true) {
							var fun = function fun() {
								i--;
								if(i === 0) {
									$('#get-code').text("获取验证码").css('background-color', "#11bdc5");
									clearInterval(time);
									_this.fgetc = true;
								} else {
									$('#get-code').text(i + "s").css('background-color', "#c2c2c2");
								}
							};

							_this.fgetc = false;
							var i = 60;
							$('#get-code').text(i + 's').css('background-color', "#c2c2c2");
							var time = setInterval(fun, 1000);
						}
					}, function(data) {
						data['code']['0'] === '160040' ? alert('当天短信发送次数超限') : alert(data.msg, "提示", "确定");
					});
				}
			},
			toAgree: function toAgree() {
				this.$router.push({
					path: '/agree'
				});
				$(".cover-box").toggle().find('.login-action').eq(1).hide();
			},
			// 验证用户名、手机号、验证码、密码
			checkver: function checkver(e) {
				var _this = this,
					a,
					b,
					c,
					d,
					username = _this.rusername,
					phone = _this.reginum,
					code = _this.identifying,
					password = _this.rpassword,
					invite = _this.inviter || '',
					reg = /^1[3|4|5|7|8][0-9]{9}$/,
					pat1 = /^[0-9]{4}$/,
					pat2 = /^[a-zA-Z0-9]{6,20}$/,
					pat3 = /^[a-zA-Z0-9]{5,20}$/,
					res = reg.test(phone),
					res1 = pat1.test(code),
					res2 = pat2.test(password),
					res3 = pat3.test(username);
				if(username == null || username == '') {
					_this.rusername_hint = '请输入用户名';
					return;
				} else if(res3) {
					_this.rusername_hint = "";
					d = 1;
				} else {
					_this.rusername_hint = '用户名不符合要求';
					return;
				}
				if(password == null || password == '') {
					_this.rpassword_hint = "密码不能为空";
					return;
				} else if(res2) {
					_this.rpassword_hint = "";
					a = 1;
				} else {
					_this.rpassword_hint = "密码不符合要求";
					return;
				}
				if(phone == null || phone == '') {
					_this.reginum_hint = "账户不能为空";
					return;
				} else if(res) {
					_this.reginum_hint = "";
					c = 1;
				} else {
					_this.reginum_hint = "账户不符合要求";
					return;
				}
				if(code == null || code == '') {
					_this.identifying_hint = "验证码不能为空";
					return;
				} else if(res1) {
					_this.identifying_hint = "";
					b = 1;
				} else {
					_this.identifying_hint = "验证码不符合要求";
					return;
				}
				if(a == 1 && b == 1 && c == 1 && d == 1) {
					_this.check_register(username, phone, code, password, invite);
				}
			},
			//点击有账号页面跳转到登录页面
			rightLogin: function(e) {
				$(".logined.login-action").removeClass("display-none");
	            $(".register").css("display","none");
	            $(".login2").removeClass("active");
	            $(".login1").addClass("active");
			}
		}
	};

	function checkver_test() {
		var _this = this,
			c,
			user = _this.reginum,
			reg = /^1[3|4|5|7|8][0-9]{9}$/,
			res = reg.test(user);
		if(user == null || user == '') {
			_this.reginum_hint = "手机号不能为空";
			return;
		} else if(res) {
			_this.reginum_hint = "";
			c = 1;
		} else {
			_this.reginum_hint = "手机号不符合要求";
			return;
		}
		if(c == 1) return true;
	}
</script>